package com.leps.nq.tx.core;

import com.leps.nq.common.util.DataUtils;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class ReadRequest extends AbstractRequest {
    final static byte[] DEFAULT_HEAD = DataUtils.hexStrToBytes("CC CC");
    final static byte[] DEFAULT_TAIL = DataUtils.hexStrToBytes("CC CC 10 16");

    public ReadRequest(String ip, int port, String plcPwdStr) {
        super(ip, port, plcPwdStr);
        this.head = DEFAULT_HEAD;
        this.tail = DEFAULT_TAIL;
    }

    public byte[] getDatagram() {
        byte[] pwd = plcPwdStr.getBytes();
        byte[] datagram = new byte[head.length + pwd.length + tail.length];
        System.arraycopy(head, 0, datagram, 0, head.length);
        System.arraycopy(pwd, 0, datagram, head.length, pwd.length);
        System.arraycopy(tail, 0, datagram, head.length + pwd.length, tail.length);
        return datagram;
    }

    public String getDatagramHexStr() {
        return DataUtils.bytesToHexStr(this.getDatagram());
    }

}
